package com.bluesmart.mia;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ForgetActivity extends BaseActivity implements OnClickListener
{
	int seconds = 30;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget);
		
		((TextView) findViewById(R.id.center)).setText(getString(R.string.forget));
		((Button) findViewById(R.id.right))
				.setCompoundDrawablesWithIntrinsicBounds(0, 0,
						R.drawable.a01, 0);
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.getcode).setOnClickListener(this);
		
		 ((EditText)findViewById(R.id.email)).addTextChangedListener(new TextWatcher()
		 {

			@Override
			public void afterTextChanged(Editable arg0) {
				String phone = arg0.toString();
				if(Utils.isMobileNO(phone))
				{
					findViewById(R.id.code_layout).setVisibility(View.VISIBLE);
				}
				else
				{
					findViewById(R.id.code_layout).setVisibility(View.GONE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				
			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				
			}
		 });
	}

	@Override
	public void onClick(View arg0) {
		if(R.id.left == arg0.getId()) finish();
		else if(R.id.right == arg0.getId())
		{
			sendHttpRequest();
		}
		else if(R.id.getcode == arg0.getId())
		{
			sendCodeRequest();
		}
	}
	
	private void sendHttpRequest() {
		final String email = ((EditText)findViewById(R.id.email)).getText().toString();
		final String pwd = ((EditText)findViewById(R.id.pwd)).getText().toString();
		final String pwd0 = ((EditText)findViewById(R.id.pwd0)).getText().toString();
		String code = ((EditText)findViewById(R.id.code)).getText().toString();
		
		if(email.length() == 0 || pwd.length() == 0 || pwd0.length() == 0)
		{
			ToastUtil.showToastWithAlertPic(R.string.all_info);
			return;
		}
		
		if(!Utils.isEmail(email) && !Utils.isMobileNO(email))
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_email);
			return;
		}
		
		String vetifyCode = PreferencesUtils.getString(this, "sms_code");
		if(findViewById(R.id.code_layout).getVisibility() == View.VISIBLE && !vetifyCode.equals(code))
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_code);
			return;
		}
		
		if(!pwd.equals(pwd0))
		{
			ToastUtil.showToastWithAlertPic(R.string.wrong_pwd);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					ToastUtil.showToastWithAlertPic(R.string.success);
					finish();
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else 
				{
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", email);
		if(findViewById(R.id.code_layout).getVisibility() == View.VISIBLE) params.put("handType", "phone");
		else params.put("handType", "email");
		params.put("newpassword", Utils.Md5(pwd));

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/ResetPassword.ashx", JSON.toJSONString(params), loginHandler);
	}
	
	private void sendCodeRequest() {
		String email = ((EditText)findViewById(R.id.email)).getText().toString();
		
		if(email.length() == 0)
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_tip);
			return;
		}
		
		if(!Utils.isEmail(email) && !Utils.isMobileNO(email))
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_email);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);

				try
				{
					if (ruser.getString("yzm").length() > 0)
					{
						String vetifyCode = ruser.getString("yzm");
						PreferencesUtils.putString(ForgetActivity.this, "sms_code", vetifyCode);
						
						final Handler handle = new Handler()
						{
							public void handleMessage(Message msg)
							{
								if(seconds == 0)
								{
									((Button)findViewById(R.id.getcode)).setText(getString(R.string.get_code));
									findViewById(R.id.getcode).setOnClickListener(ForgetActivity.this);
								}
								else
								{
									((Button)findViewById(R.id.getcode)).setText(String.valueOf(seconds));
									findViewById(R.id.getcode).setOnClickListener(null);
								}
								
								super.handleMessage(msg);
							}
						};
						
						seconds = 30;
						new Thread()
						{
							@Override
							public void run() {
								while(seconds > 0)
								{
									seconds--;
									handle.sendEmptyMessage(0);
									
									try {
										Thread.sleep(1000);
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								}
								
								handle.sendEmptyMessage(0);
							}
						}.start();
					}
					else 
					{
						ToastUtil.showToastWithAlertPic(R.string.get_code_err);
					}	
				}
				catch(Exception e)
				{
					if (ruser.getString("resCode").equals("2000")) {
						ToastUtil.showToastWithAlertPic(R.string.error_2000);
					}
					else if (ruser.getString("resCode").equals("3000")) {
						ToastUtil.showToastWithAlertPic(R.string.error_3000);
					}
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", email);
		params.put("pwd","");
		params.put("toUser", email);
		
		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/SMSHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
}
